{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "42b0c5b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame\n",
    "\n",
    "np.random.seed(0)\n",
    "df = DataFrame(np.random.randint(0, 100, [8, 3]),\n",
    "              columns=list('ABC'))\n",
    "df['year'] = [2018] * 4 + [2019] * 4\n",
    "df['quarter'] = 'Q1 Q2 Q3 Q4'.split() * 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ecc9bd8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>year</th>\n",
       "      <th>quarter</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>44</td>\n",
       "      <td>47</td>\n",
       "      <td>64</td>\n",
       "      <td>2018</td>\n",
       "      <td>Q1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>67</td>\n",
       "      <td>67</td>\n",
       "      <td>9</td>\n",
       "      <td>2018</td>\n",
       "      <td>Q2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>83</td>\n",
       "      <td>21</td>\n",
       "      <td>36</td>\n",
       "      <td>2018</td>\n",
       "      <td>Q3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>87</td>\n",
       "      <td>70</td>\n",
       "      <td>88</td>\n",
       "      <td>2018</td>\n",
       "      <td>Q4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>88</td>\n",
       "      <td>12</td>\n",
       "      <td>58</td>\n",
       "      <td>2019</td>\n",
       "      <td>Q1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>65</td>\n",
       "      <td>39</td>\n",
       "      <td>87</td>\n",
       "      <td>2019</td>\n",
       "      <td>Q2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>46</td>\n",
       "      <td>88</td>\n",
       "      <td>81</td>\n",
       "      <td>2019</td>\n",
       "      <td>Q3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>37</td>\n",
       "      <td>25</td>\n",
       "      <td>77</td>\n",
       "      <td>2019</td>\n",
       "      <td>Q4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C  year quarter\n",
       "0  44  47  64  2018      Q1\n",
       "1  67  67   9  2018      Q2\n",
       "2  83  21  36  2018      Q3\n",
       "3  87  70  88  2018      Q4\n",
       "4  88  12  58  2019      Q1\n",
       "5  65  39  87  2019      Q2\n",
       "6  46  88  81  2019      Q3\n",
       "7  37  25  77  2019      Q4"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ae494b6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>year</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>quarter</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Q1</th>\n",
       "      <td>44</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q2</th>\n",
       "      <td>67</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q3</th>\n",
       "      <td>83</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q4</th>\n",
       "      <td>87</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "year     2018  2019\n",
       "quarter            \n",
       "Q1         44    88\n",
       "Q2         67    65\n",
       "Q3         83    46\n",
       "Q4         87    37"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index='quarter', columns='year', values='A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b0ddec63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>year</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>quarter</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Q1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "year     2018  2019\n",
       "quarter            \n",
       "Q1          1     1\n",
       "Q2          1     1\n",
       "Q3          1     1\n",
       "Q4          1     1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index='quarter', columns='year',\n",
    "               values='A', sort=False, aggfunc=np.size)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a0e9c628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Issue Date\n",
       "01/02/2020 12:00:00 AM    MAZDA\n",
       "01/02/2020 12:00:00 AM    TOYOT\n",
       "01/02/2020 12:00:00 AM    NISSA\n",
       "01/02/2020 12:00:00 AM     FORD\n",
       "01/02/2020 12:00:00 AM      HIN\n",
       "01/02/2020 12:00:00 AM     FORD\n",
       "01/02/2020 12:00:00 AM     FORD\n",
       "01/02/2020 12:00:00 AM    KENWO\n",
       "01/02/2020 12:00:00 AM    CHRYS\n",
       "01/02/2020 12:00:00 AM    TOYOT\n",
       "01/02/2020 12:00:00 AM    TOYOT\n",
       "01/02/2020 12:00:00 AM      GMC\n",
       "01/02/2020 12:00:00 AM      VPG\n",
       "01/02/2020 12:00:00 AM    ME/BE\n",
       "01/02/2020 12:00:00 AM    INFIN\n",
       "01/02/2020 12:00:00 AM    ACURA\n",
       "01/02/2020 12:00:00 AM      KIA\n",
       "01/02/2020 12:00:00 AM    INFIN\n",
       "01/02/2020 12:00:00 AM    NISSA\n",
       "01/02/2020 12:00:00 AM    HONDA\n",
       "01/02/2020 12:00:00 AM    TOYOT\n",
       "01/02/2020 12:00:00 AM    NISSA\n",
       "01/02/2020 12:00:00 AM    BUICK\n",
       "01/02/2020 12:00:00 AM    NISSA\n",
       "01/02/2020 12:00:00 AM    NISSA\n",
       "01/02/2020 12:00:00 AM    DODGE\n",
       "01/02/2020 12:00:00 AM    NISSA\n",
       "01/02/2020 12:00:00 AM    HONDA\n",
       "01/02/2020 12:00:00 AM    HONDA\n",
       "01/02/2020 12:00:00 AM     MINI\n",
       "Name: Vehicle Make, dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = '../data/nyc-parking-violations-2020.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['Date First Observed', 'Registration State', 'Plate ID',\n",
    "                        'Issue Date', 'Vehicle Make', 'Street Name', 'Vehicle Color'])\n",
    "df = df.set_index('Issue Date') \n",
    "df.loc['01/02/2020 12:00:00 AM', 'Vehicle Make'].head(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b48d77ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Issue Date,Vehicle Make\\n01/02/2020 12:00:00 AM,MAZDA\\n01/02/2020 12:00:00 AM,TOYOT\\n01/02/2020 12:00:00 AM,NISSA\\n01/02/2020 12:00:00 AM,FORD\\n01/02/2020 12:00:00 AM,HIN\\n01/02/2020 12:00:00 AM,FORD\\n01/02/2020 12:00:00 AM,FORD\\n01/02/2020 12:00:00 AM,KENWO\\n01/02/2020 12:00:00 AM,CHRYS\\n01/02/2020 12:00:00 AM,TOYOT\\n01/02/2020 12:00:00 AM,TOYOT\\n01/02/2020 12:00:00 AM,GMC\\n01/02/2020 12:00:00 AM,VPG\\n01/02/2020 12:00:00 AM,ME/BE\\n01/02/2020 12:00:00 AM,INFIN\\n01/02/2020 12:00:00 AM,ACURA\\n01/02/2020 12:00:00 AM,KIA\\n01/02/2020 12:00:00 AM,INFIN\\n01/02/2020 12:00:00 AM,NISSA\\n01/02/2020 12:00:00 AM,HONDA\\n01/02/2020 12:00:00 AM,TOYOT\\n01/02/2020 12:00:00 AM,NISSA\\n01/02/2020 12:00:00 AM,BUICK\\n01/02/2020 12:00:00 AM,NISSA\\n01/02/2020 12:00:00 AM,NISSA\\n01/02/2020 12:00:00 AM,DODGE\\n01/02/2020 12:00:00 AM,NISSA\\n01/02/2020 12:00:00 AM,HONDA\\n01/02/2020 12:00:00 AM,HONDA\\n01/02/2020 12:00:00 AM,MINI\\n'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['01/02/2020 12:00:00 AM', 'Vehicle Make'].head(30).to_csv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cc9df875",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method to_csv in module pandas.core.generic:\n",
      "\n",
      "to_csv(path_or_buf: 'FilePath | WriteBuffer[bytes] | WriteBuffer[str] | None' = None, sep: 'str' = ',', na_rep: 'str' = '', float_format: 'str | Callable | None' = None, columns: 'Sequence[Hashable] | None' = None, header: 'bool_t | list[str]' = True, index: 'bool_t' = True, index_label: 'IndexLabel | None' = None, mode: 'str' = 'w', encoding: 'str | None' = None, compression: 'CompressionOptions' = 'infer', quoting: 'int | None' = None, quotechar: 'str' = '\"', lineterminator: 'str | None' = None, chunksize: 'int | None' = None, date_format: 'str | None' = None, doublequote: 'bool_t' = True, escapechar: 'str | None' = None, decimal: 'str' = '.', errors: 'str' = 'strict', storage_options: 'StorageOptions' = None) -> 'str | None' method of pandas.core.frame.DataFrame instance\n",
      "    Write object to a comma-separated values (csv) file.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    path_or_buf : str, path object, file-like object, or None, default None\n",
      "        String, path object (implementing os.PathLike[str]), or file-like\n",
      "        object implementing a write() function. If None, the result is\n",
      "        returned as a string. If a non-binary file object is passed, it should\n",
      "        be opened with `newline=''`, disabling universal newlines. If a binary\n",
      "        file object is passed, `mode` might need to contain a `'b'`.\n",
      "    \n",
      "        .. versionchanged:: 1.2.0\n",
      "    \n",
      "           Support for binary file objects was introduced.\n",
      "    \n",
      "    sep : str, default ','\n",
      "        String of length 1. Field delimiter for the output file.\n",
      "    na_rep : str, default ''\n",
      "        Missing data representation.\n",
      "    float_format : str, Callable, default None\n",
      "        Format string for floating point numbers. If a Callable is given, it takes\n",
      "        precedence over other numeric formatting parameters, like decimal.\n",
      "    columns : sequence, optional\n",
      "        Columns to write.\n",
      "    header : bool or list of str, default True\n",
      "        Write out the column names. If a list of strings is given it is\n",
      "        assumed to be aliases for the column names.\n",
      "    index : bool, default True\n",
      "        Write row names (index).\n",
      "    index_label : str or sequence, or False, default None\n",
      "        Column label for index column(s) if desired. If None is given, and\n",
      "        `header` and `index` are True, then the index names are used. A\n",
      "        sequence should be given if the object uses MultiIndex. If\n",
      "        False do not print fields for index names. Use index_label=False\n",
      "        for easier importing in R.\n",
      "    mode : str, default 'w'\n",
      "        Python write mode. The available write modes are the same as\n",
      "        :py:func:`open`.\n",
      "    encoding : str, optional\n",
      "        A string representing the encoding to use in the output file,\n",
      "        defaults to 'utf-8'. `encoding` is not supported if `path_or_buf`\n",
      "        is a non-binary file object.\n",
      "    compression : str or dict, default 'infer'\n",
      "        For on-the-fly compression of the output data. If 'infer' and 'path_or_buf' is\n",
      "        path-like, then detect compression from the following extensions: '.gz',\n",
      "        '.bz2', '.zip', '.xz', '.zst', '.tar', '.tar.gz', '.tar.xz' or '.tar.bz2'\n",
      "        (otherwise no compression).\n",
      "        Set to ``None`` for no compression.\n",
      "        Can also be a dict with key ``'method'`` set\n",
      "        to one of {``'zip'``, ``'gzip'``, ``'bz2'``, ``'zstd'``, ``'tar'``} and other\n",
      "        key-value pairs are forwarded to\n",
      "        ``zipfile.ZipFile``, ``gzip.GzipFile``,\n",
      "        ``bz2.BZ2File``, ``zstandard.ZstdCompressor`` or\n",
      "        ``tarfile.TarFile``, respectively.\n",
      "        As an example, the following could be passed for faster compression and to create\n",
      "        a reproducible gzip archive:\n",
      "        ``compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}``.\n",
      "    \n",
      "            .. versionadded:: 1.5.0\n",
      "                Added support for `.tar` files.\n",
      "    \n",
      "        .. versionchanged:: 1.0.0\n",
      "    \n",
      "           May now be a dict with key 'method' as compression mode\n",
      "           and other entries as additional compression options if\n",
      "           compression mode is 'zip'.\n",
      "    \n",
      "        .. versionchanged:: 1.1.0\n",
      "    \n",
      "           Passing compression options as keys in dict is\n",
      "           supported for compression modes 'gzip', 'bz2', 'zstd', and 'zip'.\n",
      "    \n",
      "        .. versionchanged:: 1.2.0\n",
      "    \n",
      "            Compression is supported for binary file objects.\n",
      "    \n",
      "        .. versionchanged:: 1.2.0\n",
      "    \n",
      "            Previous versions forwarded dict entries for 'gzip' to\n",
      "            `gzip.open` instead of `gzip.GzipFile` which prevented\n",
      "            setting `mtime`.\n",
      "    \n",
      "    quoting : optional constant from csv module\n",
      "        Defaults to csv.QUOTE_MINIMAL. If you have set a `float_format`\n",
      "        then floats are converted to strings and thus csv.QUOTE_NONNUMERIC\n",
      "        will treat them as non-numeric.\n",
      "    quotechar : str, default '\\\"'\n",
      "        String of length 1. Character used to quote fields.\n",
      "    lineterminator : str, optional\n",
      "        The newline character or character sequence to use in the output\n",
      "        file. Defaults to `os.linesep`, which depends on the OS in which\n",
      "        this method is called ('\\\\n' for linux, '\\\\r\\\\n' for Windows, i.e.).\n",
      "    \n",
      "        .. versionchanged:: 1.5.0\n",
      "    \n",
      "            Previously was line_terminator, changed for consistency with\n",
      "            read_csv and the standard library 'csv' module.\n",
      "    \n",
      "    chunksize : int or None\n",
      "        Rows to write at a time.\n",
      "    date_format : str, default None\n",
      "        Format string for datetime objects.\n",
      "    doublequote : bool, default True\n",
      "        Control quoting of `quotechar` inside a field.\n",
      "    escapechar : str, default None\n",
      "        String of length 1. Character used to escape `sep` and `quotechar`\n",
      "        when appropriate.\n",
      "    decimal : str, default '.'\n",
      "        Character recognized as decimal separator. E.g. use ',' for\n",
      "        European data.\n",
      "    errors : str, default 'strict'\n",
      "        Specifies how encoding and decoding errors are to be handled.\n",
      "        See the errors argument for :func:`open` for a full list\n",
      "        of options.\n",
      "    \n",
      "        .. versionadded:: 1.1.0\n",
      "    \n",
      "    storage_options : dict, optional\n",
      "        Extra options that make sense for a particular storage connection, e.g.\n",
      "        host, port, username, password, etc. For HTTP(S) URLs the key-value pairs\n",
      "        are forwarded to ``urllib.request.Request`` as header options. For other\n",
      "        URLs (e.g. starting with \"s3://\", and \"gcs://\") the key-value pairs are\n",
      "        forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more\n",
      "        details, and for more examples on storage options refer `here\n",
      "        <https://pandas.pydata.org/docs/user_guide/io.html?\n",
      "        highlight=storage_options#reading-writing-remote-files>`_.\n",
      "    \n",
      "        .. versionadded:: 1.2.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    None or str\n",
      "        If path_or_buf is None, returns the resulting csv format as a\n",
      "        string. Otherwise returns None.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    read_csv : Load a CSV file into a DataFrame.\n",
      "    to_excel : Write DataFrame to an Excel file.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'],\n",
      "    ...                    'mask': ['red', 'purple'],\n",
      "    ...                    'weapon': ['sai', 'bo staff']})\n",
      "    >>> df.to_csv(index=False)\n",
      "    'name,mask,weapon\\nRaphael,red,sai\\nDonatello,purple,bo staff\\n'\n",
      "    \n",
      "    Create 'out.zip' containing 'out.csv'\n",
      "    \n",
      "    >>> compression_opts = dict(method='zip',\n",
      "    ...                         archive_name='out.csv')  # doctest: +SKIP\n",
      "    >>> df.to_csv('out.zip', index=False,\n",
      "    ...           compression=compression_opts)  # doctest: +SKIP\n",
      "    \n",
      "    To write a csv file to a new folder or nested folder you will first\n",
      "    need to create it using either Pathlib or os:\n",
      "    \n",
      "    >>> from pathlib import Path  # doctest: +SKIP\n",
      "    >>> filepath = Path('folder/subfolder/out.csv')  # doctest: +SKIP\n",
      "    >>> filepath.parent.mkdir(parents=True, exist_ok=True)  # doctest: +SKIP\n",
      "    >>> df.to_csv(filepath)  # doctest: +SKIP\n",
      "    \n",
      "    >>> import os  # doctest: +SKIP\n",
      "    >>> os.makedirs('folder/subfolder', exist_ok=True)  # doctest: +SKIP\n",
      "    >>> df.to_csv('folder/subfolder/out.csv')  # doctest: +SKIP\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(df.to_csv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d44d3db7",
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = '../data/sat-scores.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['Year',\n",
    "                         'State.Code',\n",
    "                         'Total.Math',\n",
    "                         'Total.Test-takers',\n",
    "                         'Total.Verbal'])\n",
    "\n",
    "df = df.set_index(['Year', 'State.Code']) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7ab1c2e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Year,State.Code,Total.Math,Total.Test-takers,Total.Verbal\\n2005,AL,559,3985,567\\n2005,AK,519,3996,523\\n2005,AZ,530,18184,526\\n2005,AR,552,1600,563\\n2005,CA,522,186552,504\\n2005,CO,560,11990,560\\n2005,CT,517,34313,517\\n2005,DE,502,6257,503\\n2005,DC,478,3622,490\\n2005,FL,498,93505,498\\n2005,GA,496,59842,497\\n2005,HI,516,7878,490\\n2005,ID,542,3506,544\\n2005,IL,606,12970,594\\n2005,IN,508,41553,504\\n2005,IA,608,1671,596\\n2005,KS,588,2667,585\\n2005,KY,559,4666,561\\n2005,LA,562,3290,565\\n2005,ME,505,10985,509\\n2005,MD,515,44458,511\\n2005,MA,527,59104,520\\n2005,MI,579,10965,568\\n2005,MN,597,6470,592\\n2005,MS,554,1106,564\\n2005,MO,588,4413,588\\n2005,MT,540,3326,540\\n2005,NE,579,1684,574\\n2005,NV,513,7065,508\\n2005,NH,525,12350,525\\n2005,NJ,517,81479,503\\n2005,NM,547,2536,558\\n2005,NY,511,154897,497\\n2005,NC,511,53314,499\\n2005,ND,605,351,590\\n2005,OH,543,35155,539\\n2005,OK,563,2699,570\\n2005,OR,528,19535,526\\n2005,PA,503,104155,501\\n2005,PR,458,1891,462\\n2005,RI,505,8200,503\\n2005,SC,499,23488,494\\n2005,SD,589,450,589\\n2005,TN,563,7642,572\\n2005,TX,502,133115,493\\n2005,UT,557,2112,566\\n2005,VT,517,5548,521\\n2005,VI,405,898,422\\n2005,VA,511,3480,523\\n2005,WA,534,35020,532\\n2005,WI,599,4230,592\\n2005,WY,543,656,544\\n2006,AL,562,3879,566\\n2006,AK,519,3945,517\\n2006,AZ,529,18615,520\\n2006,AR,568,1489,574\\n2006,CA,520,191740,501\\n2006,CO,565,11806,557\\n2006,CT,517,34522,512\\n2006,DE,502,6406,495\\n2006,DC,473,3593,487\\n2006,FL,497,94601,496\\n2006,GA,496,58309,494\\n2006,HI,511,7821,481\\n2006,ID,545,3163,542\\n2006,IL,609,12694,591\\n2006,IN,511,41568,498\\n2006,IA,614,1477,603\\n2006,KS,591,2545,582\\n2006,KY,563,4417,561\\n2006,LA,573,2622,571\\n2006,ME,503,10895,501\\n2006,MD,510,45231,503\\n2006,MA,524,59529,513\\n2006,MI,584,10405,569\\n'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(75).to_csv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ef365ea7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.11/site-packages/pandas/core/internals/blocks.py:2323: RuntimeWarning: invalid value encountered in cast\n",
      "  values = values.astype(str)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "',Age,Height,Team,Year,Season,Sport,Medal\\n90722,23.0,160.0,United States,2002,Winter,Freestyle Skiing,\\n21261,14.0,142.0,China,1996,Summer,Gymnastics,\\n89632,25.0,174.0,China,2008,Summer,Football,\\n150341,22.0,170.0,Great Britain,2004,Summer,Swimming,\\n241146,24.0,163.0,United States,1992,Summer,Athletics,\\n79399,34.0,178.0,Great Britain,1996,Summer,Shooting,\\n165043,18.0,183.0,France,2008,Summer,Swimming,\\n138245,19.0,198.0,China,1996,Summer,Basketball,\\n185088,27.0,178.0,United States,1996,Summer,Handball,\\n202888,22.0,171.0,United States,1992,Summer,Gymnastics,\\n189582,23.0,186.0,Great Britain,2016,Summer,Table Tennis,\\n60422,23.0,168.0,France,1992,Winter,Alpine Skiing,\\n61523,20.0,175.0,United States,1984,Summer,Basketball,Gold\\n266653,22.0,174.0,China,1984,Summer,Athletics,\\n41254,20.0,180.0,United States,1980,Winter,Ice Hockey,Gold\\n192712,20.0,,India,1992,Summer,Boxing,\\n27195,26.0,171.0,Switzerland,1994,Winter,Alpine Skiing,\\n193744,27.0,170.0,United States,2016,Summer,Sailing,\\n121607,19.0,183.0,United States,1984,Summer,Cycling,Bronze\\n267959,20.0,170.0,Switzerland,1992,Winter,Nordic Combined,\\n196395,16.0,157.0,France,2000,Summer,Gymnastics,\\n108394,21.0,175.0,China,2008,Summer,Synchronized Swimming,Bronze\\n145335,16.0,152.0,France,1992,Summer,Gymnastics,\\n44301,34.0,160.0,United States,2014,Winter,Freestyle Skiing,\\n124270,25.0,180.0,France,2004,Summer,Handball,\\n142785,20.0,172.0,China,1992,Summer,Swimming,\\n182794,28.0,169.0,France,2002,Winter,Alpine Skiing,\\n37558,19.0,173.0,United States,2010,Winter,Short Track Speed Skating,Bronze\\n215080,25.0,189.0,Great Britain,2008,Summer,Athletics,\\n239906,22.0,165.0,China,1988,Summer,Athletics,\\n'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = '../data/olympic_athlete_events.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['Age', 'Height', 'Team', 'Year', 'Season', 'Sport', 'Medal'])\n",
    "\n",
    "df = df.loc[df['Team'].isin(['Great Britain', 'France', 'United States', 'Switzerland', 'China', 'India'])]\n",
    "df = df.loc[df['Year'] >= 1980]\n",
    "\n",
    "np.random.seed(0)\n",
    "df.sample(30).to_csv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8704cac7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
